﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using C1.Phone.Chart;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Windows.Data;
using Microsoft.Phone.Shell;

namespace ControlExplorer.Controls.Chart
{
    public partial class Dynamic
        : UserControl, IExposeApplicationBarItems
    {
        ObservableCollection<Point> _pts = new ObservableCollection<Point>();
        int counter = 0;
        Random rnd = new Random();
        DispatcherTimer dt;
        int nMaxPoints = 40;
        int nAddPoints = 1;

        public Dynamic()
        {
            InitializeComponent();

            chart.DataContext = _pts;
            chart.ChartType = ChartType.Line;

            XYDataSeries ds = new XYDataSeries()
            {
                XValueBinding = new Binding("X"),
                ValueBinding = new Binding("Y"),
                ConnectionStrokeThickness = 1,
                Label = "raw",
                ConnectionStroke = new SolidColorBrush(Colors.DarkGray)
            };

            chart.Data.Children.Add(ds);

            chart.View.AxisY.Min = -1000;
            chart.View.AxisY.Max = 1000;

            dt = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(0.2) };
            dt.Tick += (s, e) => Update();
            dt.Start();


            btnTimer.Click += Button_Click;
            UpdateButton();
        }

        void Update()
        {
            chart.BeginUpdate();

            int cnt = nAddPoints;
            for (int i = 0; i < cnt; i++)
            {
                double r = rnd.NextDouble();
                double y = (10 * r * Math.Sin(0.1 * counter) * Math.Sin(0.6 * rnd.NextDouble() * counter));
                _pts.Add(new Point(counter++, y * 100));
            }

            int ndel = _pts.Count - nMaxPoints;
            if (ndel > 0)
                for (int i = 0; i < ndel; i++)
                    _pts.RemoveAt(0);

            chart.EndUpdate();
        }

        private void Button_Click(object sender, EventArgs e)
        {
            UpdateButton();
        }

        private void UpdateButton()
        {
            if (dt.IsEnabled)
            {
                dt.Stop();
                btnTimer.Text = "Start";
                btnTimer.IconUri = new Uri("/Resources/AppBar/play.png", UriKind.Relative);
            }
            else
            {
                dt.Start();
                btnTimer.Text = "Pause";
                btnTimer.IconUri = new Uri("/Resources/AppBar/pause.png", UriKind.Relative);
            }
        }

        internal void StopTimer()
        {
            if (dt.IsEnabled)
            {
                dt.Stop();
                UpdateButton();
            }
        }

        ApplicationBarIconButton btnTimer = new ApplicationBarIconButton();

        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get 
            {
                yield return btnTimer;
            }
        }

        #endregion
    }
}